package org.file_service.service.serviceimpl;

import lombok.extern.slf4j.Slf4j;
import org.file_service.dao.HistoryMapper;
import org.file_service.entity.ChaEr;
import org.file_service.entity.History;
import org.file_service.entity.Huxing;
import org.file_service.entity.MonthPrice;
import org.file_service.lang.Result;
import org.file_service.model.*;
import org.file_service.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class HistoryServiceImpl implements HistoryService {

    @Autowired
    public HistoryMapper historyMapper;

    @Override
    public Result allRecord() {
        List<History> allRecord = historyMapper.allRecord();
        if (allRecord.size() != 0)
            return Result.suc(allRecord);
        else return Result.fail("历史记录为空");
    }

    @Override
    public Result two(Integer userId, Integer historyId) {
        List<Two> twos = historyMapper.two(userId, historyId);
        if (twos.size() != 0) {
            Map<String, Double> map = new HashMap<>();
            for (Two t : twos) {
                map.put(t.getZone() , t.getAvg_price());
            }
            return Result.suc(map);
        } else return Result.fail("数据出错或不存在");
    }

    @Override
    public Result three(Integer userId, Integer historyId) {
        List<Three> threes = historyMapper.getThree(userId, historyId);
        Map<String, List<Three>> zonemap = new HashMap<>();
        for (Three t : threes) {
            if (zonemap.containsKey(t.getZone())) {
                List<Three> list = zonemap.get(t.getZone());
                list.add(t);
                zonemap.put(t.getZone(), list);
            }else {
                List<Three> list = new ArrayList<>();
                list.add(t);
                zonemap.put(t.getZone(), list);
            }
        }

        Map<String, Map<String, Integer>> resultMap = new HashMap<>();
        for (String key : zonemap.keySet()) {
            Map<String, Integer> ListMap = new HashMap<>();//朝向—数量
            List<Three> list = zonemap.get(key); //list是地区列表
            for (Three three : list) {
                ListMap.put(three.getToward(),three.getCount());
            }
            resultMap.put(key,ListMap);
        }
        if (resultMap.size() != 0)
            return Result.suc(resultMap);
        else return Result.fail("空");
    }


    @Override
    public Result four(Integer userId, Integer historyId) {
        List<Four> fours = historyMapper.getFour(userId, historyId);
        Map<String, List<Four>> zonemap = new HashMap<>();
        for (Four f : fours) {
            if (zonemap.containsKey(f.getZone())) {
                List<Four> list = zonemap.get(f.getZone());
                list.add(f);
                zonemap.put(f.getZone(), list);
            }else {
                List<Four> list = new ArrayList<>();
                list.add(f);
                zonemap.put(f.getZone(), list);
            }
        }

        Map<String, Map<String, Integer>> resultMap = new HashMap<>();
        for (String key : zonemap.keySet()) {
            Map<String, Integer> ListMap = new HashMap<>();//朝向—数量
            List<Four> list = zonemap.get(key); //list是地区列表
            for (Four four : list) {
                ListMap.put(four.getFurnish(),four.getCount());
            }
            resultMap.put(key,ListMap);
        }
        if (resultMap.size() != 0)
            return Result.suc(resultMap);
        else return Result.fail("空");
    }

    @Override
    public Result five(Integer userId, Integer historyId) {
        List<FiveSix> fiveSixes = historyMapper.getFive(userId, historyId);
        Map<String, List<FiveSix>> zonemap = new HashMap<>();
        for (FiveSix f : fiveSixes) {
            if (zonemap.containsKey(f.getZone())) {
                List<FiveSix> list = zonemap.get(f.getZone());
                list.add(f);
                zonemap.put(f.getZone(), list);
            }else {
                List<FiveSix> list = new ArrayList<>();
                list.add(f);
                zonemap.put(f.getZone(), list);
            }
        }

        Map<String, Map<String, Integer>> resultMap = new HashMap<>();
        for (String key : zonemap.keySet()) {
            Map<String, Integer> ListMap = new HashMap<>();//朝向—数量
            List<FiveSix> list = zonemap.get(key); //list是地区列表
            for (FiveSix fiveSix : list) {
                ListMap.put(fiveSix.getFloor(),fiveSix.getCount());
            }
            resultMap.put(key,ListMap);
        }
        if (resultMap.size() != 0)
            return Result.suc(resultMap);
        else return Result.fail("空");
    }

    @Override
    public Result six(Integer userId, Integer historyId) {
        List<FiveSix> fiveSixes = historyMapper.getFive(userId, historyId);
        Map<String, List<FiveSix>> zonemap = new HashMap<>();
        for (FiveSix f : fiveSixes) {
            if (zonemap.containsKey(f.getZone())) {
                List<FiveSix> list = zonemap.get(f.getZone());
                list.add(f);
                zonemap.put(f.getZone(), list);
            }else {
                List<FiveSix> list = new ArrayList<>();
                list.add(f);
                zonemap.put(f.getZone(), list);
            }
        }

        Map<String, Map<String, Integer>> resultMap = new HashMap<>();
        for (String key : zonemap.keySet()) {
            Map<String, Integer> ListMap = new HashMap<>();//朝向—数量
            List<FiveSix> list = zonemap.get(key); //list是地区列表
            for (FiveSix fiveSix : list) {
                ListMap.put(fiveSix.getFloor(),fiveSix.getAvg_price());
            }
            resultMap.put(key,ListMap);
        }
        if (resultMap.size() != 0)
            return Result.suc(resultMap);
        else return Result.fail("空");
    }


    @Override
    public Result seven(Integer userId, Integer historyId) {
        List<Seven> sevens = historyMapper.seven(userId, historyId);
        if (sevens.size() != 0) {
            Map<String, Map> map = new HashMap<>();
            List<String> keys = new ArrayList<>();
            for (Seven s : sevens) {
                if (keys.contains(s.getZone())) {
                    continue;
                } else {
                    keys.add(s.getZone());
                }
            }
            Integer i = 0;
            Map<String, Integer> map1;
            map1 = new HashMap<>();
            for (Seven s : sevens) {
                if (keys.get(i).equals(s.getZone())) {
                    map1.put(s.getQuarter(), s.getCount());
                } else {
                    map.put(keys.get(i), map1);
                    map1 = new HashMap<>();
                    map1.put(s.getQuarter(), s.getCount());
                    i += 1;
                }
                map.put(keys.get(i), map1);
            }
            return Result.suc(map);
        } else return Result.fail("数据出错或不存在");
    }

    @Override
    public Result eight(Integer userId, Integer historyId) {
        List<Eight> eights1 = historyMapper.eight1(userId, historyId);
        Map<String, Integer> map1 = new HashMap<>();
        List<String> l = new ArrayList<>();
        for (Eight e : eights1) {
            if (l.contains(e.getHouse_time())) {
                continue;
            } else {
                l.add(e.getHouse_time());
            }
            map1.put(e.getHouse_time(), e.getCount());
        }
        List<Map> l1 = new ArrayList<>();
        l1.add(map1);
        System.out.println(l + " " + map1);
        Map<String, Map> map2 = new HashMap<>();
        List<List<Eight>> eights2 = new ArrayList<>();
        List<String> keys = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            List<Eight> e = historyMapper.eight2(userId, historyId, l.get(i));
            eights2.add(e);
        }
        for (List<Eight> e1 : eights2) {
            for (Eight eight1 : e1) {
                if (keys.contains(eight1.getZone())) {
                    continue;
                } else {
                    keys.add(eight1.getZone());
                }
            }
        }
        map1 = new HashMap<>();
        for (int i = 0; i < keys.size(); i++) {
            for (List<Eight> e1 : eights2) {
                for (Eight eight : e1) {
                    if (keys.get(i).equals(eight.getZone())) {
                        map1.put(eight.getHouse_time(), eight.getCount());
                    }
                }
            }
            map2.put(keys.get(i), map1);
            map1 = new HashMap<>();
        }
        l1.add(map2);
        return Result.suc(l1);
    }

    @Override
    public Result huxingDistributionAndPrice(int historyId) {
        List<Huxing> HuxingRecord = historyMapper.huxingDistributionAndPrice(historyId);
//        List<String> zone = new ArrayList<>();
        Map<String, List<Huxing>> zoneMap = new HashMap<>();
        for (Huxing h : HuxingRecord) {
            if (zoneMap.containsKey(h.getZone())) {
                List<Huxing> list = zoneMap.get(h.getZone());
                h.setAvg_price(Double.valueOf(String.format("%.2f",h.getAvg_price())));
                list.add(h);
                zoneMap.put(h.getZone(), list);
            } else {
                List<Huxing> list = new ArrayList<>();
                list.add(h);
                zoneMap.put(h.getZone(), list);
            }
        }
        Map<String, List<Huxing>> resultmap = new HashMap<>();
        for (String key : zoneMap.keySet()) {
            List<Huxing> list = zoneMap.get(key);
//            Collections.sort(list);
            List<Huxing> list1 = new ArrayList<>();
//            list1.add(list.get(0));
//            list1.add(list.get(1));
//            list1.add(list.get(2));
            for (Huxing h : list) {
                if (h.getType().equals("3室1厅") || h.getType().equals("2室1厅") || h.getType().equals("1室1厅"))
                    list1.add(h);
            }
            resultmap.put(key, list1);
        }


        if (resultmap.size() != 0)
            return Result.suc(resultmap);
        else return Result.fail("历史记录为空");
    }

    @Override
    public Result monthAndPrice(int history) {
        List<MonthPrice> monthRceord = historyMapper.getMonthAndPrice(history);
        Map<String, List<MonthPrice>> zonemap = new HashMap<>();

        for (MonthPrice m : monthRceord) {
            if (zonemap.containsKey(m.getZone())) {
                List<MonthPrice> list = zonemap.get(m.getZone());
                list.add(m);
                zonemap.put(m.getZone(), list);
            } else {
                List<MonthPrice> list = new ArrayList<>();
                list.add(m);
                zonemap.put(m.getZone(), list);
            }
        }

        Map<String, Map<String, Double>> resultmap = new HashMap<>();

        for (String key : zonemap.keySet()) {
            Map<String, List<Double>> avgPriceListMap = new HashMap<>();
            Map<String, Double> avgPriceMap = new HashMap<>();
            List<MonthPrice> list = zonemap.get(key);
//            List<Double> doubles = new ArrayList<>();
            for (MonthPrice mp : list) {
                for (int i = 1; i <= 11; i += 2) {
                    if (mp.getMonth() == i || mp.getMonth() == i + 1) {
                        String temp = String.valueOf(i) + "-" + String.valueOf(i + 1) + "月";
                        if (avgPriceListMap.containsKey(temp)) {
                            List<Double> doubles = avgPriceListMap.get(temp);
                            doubles.add(mp.getAvg_price());
                            avgPriceListMap.put(temp, doubles);
                        } else {
                            List<Double> doubles = new ArrayList<>();
                            doubles.add(mp.getAvg_price());
                            avgPriceListMap.put(temp, doubles);
                        }
                        break;
                    }
                }
            }

            for (String s : avgPriceListMap.keySet()) {
                Double sum = 0.0;
                List<Double> doubles1 = avgPriceListMap.get(s);
                for (Double d : doubles1) {
                    sum += d;
                }
                avgPriceMap.put(s, sum / doubles1.size());
            }

            resultmap.put(key, avgPriceMap);
//                resultmap.put(key,)

        }


        if (resultmap.size() != 0)
            return Result.suc(resultmap);
        else return Result.fail("历史记录为空");
    }

    @Override
    public Result GuaPaiAndChengjiao(int history) {
        DecimalFormat df = new DecimalFormat(".00");
        Map<String, List<Double>> result = new HashMap<>();
        List<ChaEr> ChajiaRecord = historyMapper.getChaerPrice(history);
        for (ChaEr chaEr : ChajiaRecord) {
            List<Double> list = new ArrayList<>();
            Double spreadPrice = Double.valueOf(chaEr.getSpreadPrice());
            Double d = spreadPrice / Double.valueOf(chaEr.getCount());
            list.add(spreadPrice);
            Double dd = Double.valueOf(String.format("%.2f", d));
            list.add(dd);
            result.put(chaEr.getZone(), list);
        }

        if (result.size() != 0)
            return Result.suc(result);
        else return Result.fail("历史记录为空");
    }

}
